package com.oahcfly.chgame.egret.ui;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import com.oahcfly.chgame.core.Chao;
import com.oahcfly.chgame.egret.ui.property.BitmapLabelProperty;
import com.oahcfly.chgame.egret.ui.property.ButtonProperty;
import com.oahcfly.chgame.egret.ui.property.GroupProperty;
import com.oahcfly.chgame.egret.ui.property.UIAssetProperty;
import com.oahcfly.chgame.org.freyja.libgdx.cocostudio.ui.widget.LabelAtlas;

/**
 * 
 * 基础的UI皮肤类
 * 
 * @author haocao
 *
 */
public abstract class BaseSkinUI extends Group {

	public BaseSkinUI(Array<BaseUIProperty> propertys) {
		this.preloadRes();

		for (BaseUIProperty baseUIProperty : propertys) {
			if (baseUIProperty instanceof GroupProperty) {
				parseBaseProperty(this, baseUIProperty);
			} else if (baseUIProperty instanceof UIAssetProperty) {
				Image actor = new Image();
				parseUIAssetProperty(actor, (UIAssetProperty) baseUIProperty);
				parseBaseProperty(actor, baseUIProperty);
				this.addActor(actor);
			} else if (baseUIProperty instanceof ButtonProperty) {
				Button actor = new Button();
				parseButtonProperty(actor, (ButtonProperty) baseUIProperty);
				parseBaseProperty(actor, baseUIProperty);
				this.addActor(actor);
			} else if(baseUIProperty instanceof BitmapLabelProperty){
				LabelAtlas labelAtlas = null;
				parseBitmalLabelProperty(labelAtlas, (BitmapLabelProperty)baseUIProperty);
				parseBaseProperty(labelAtlas, baseUIProperty);
				this.addActor(labelAtlas);
			}
		}
		this.createChildComplete();
	}

	private void parseBaseProperty(Actor actor, BaseUIProperty baseUIProperty) {

		// 名称
		if (baseUIProperty._id != null) {
			actor.setName(baseUIProperty._id);
		}

		float width = baseUIProperty._width;
		float height = baseUIProperty._height;
		// 宽 高
		// 距离四个边缘的距离[优先级大于宽高设定]
		boolean layout = width > 0 || height > 0;
		if (baseUIProperty._left != -1) {
			width -= baseUIProperty._left;
			layout = true;
		}

		if (baseUIProperty._right != -1 && baseUIProperty._left != -1) {
			width = Chao.gameW;
		}

		if (baseUIProperty._top != -1 && baseUIProperty._bottom != -1) {
			height = Chao.gameH;
		}

		if (baseUIProperty._right != -1) {
			width -= baseUIProperty._right;
			layout = true;
		}

		if (baseUIProperty._left != -1) {
			width -= baseUIProperty._left;
			layout = true;
		}

		if (baseUIProperty._top != -1) {
			height -= baseUIProperty._top;
			layout = true;
		}

		if (baseUIProperty._bottom != -1) {
			layout = true;
			height -= baseUIProperty._bottom;
		}

		if (layout) {
			actor.setSize(width, height);
		}

		// 坐标转换，egret以左上角，libgdx以左下角
		float x = baseUIProperty._x;
		float y = Chao.gameH - baseUIProperty._y - actor.getHeight();
		if (baseUIProperty._left != -1) {
			x += baseUIProperty._left;
		}
		if (baseUIProperty._top != -1) {
			y -= baseUIProperty._top;
		}

		// 坐标
		actor.setPosition(x, y);

		// 垂直居中
		if (baseUIProperty._verticalCenter != -1) {
			actor.setY(Chao.gameH / 2 - actor.getHeight() / 2 - baseUIProperty._verticalCenter);
		}
		// 水平居中
		if (baseUIProperty._horizontalCenter != -1) {
			actor.setX(Chao.gameW / 2 - actor.getWidth() / 2 + baseUIProperty._horizontalCenter);
		}

	}

	private void parseUIAssetProperty(Image actor, UIAssetProperty uiAssetProperty) {
		Texture texture = ERes.getResTexture(uiAssetProperty.source);
		actor.setDrawable(new TextureRegionDrawable(new TextureRegion(texture)));
	}

	private void parseButtonProperty(Button button, ButtonProperty buttonProperty) {
		Texture upTexture = ERes.getResTexture(buttonProperty.upSource);
		Texture downTexture = ERes.getResTexture(buttonProperty.downSource);
		TextureRegionDrawable up = new TextureRegionDrawable(new TextureRegion(upTexture));
		TextureRegionDrawable down = new TextureRegionDrawable(new TextureRegion(downTexture));

		button.setStyle(new ButtonStyle(up, down, up));
		button.setSize(button.getPrefWidth(), button.getPrefHeight());
	}

	private void parseBitmalLabelProperty(LabelAtlas labelAtlas,BitmapLabelProperty bitmapLabelProperty){
	
		FntParser fntParser = new FntParser(ERes.getResUrl(bitmapLabelProperty.fntName));
		
		System.out.println("");
		//labelAtlas = new LabelAtlas(tr, tileWidth, tileHeight, startCharMap, stringValue);
	}
	
	
	
	/**
	 * 创建子元素完毕
	 */
	public abstract void createChildComplete();

	/**
	 * 预先加载使用的资源
	 */
	public abstract void preloadRes();
}
